#pragma once

#include <furi.h>
#include <furi_hal.h>
#include <gui/gui.h>
#include <gui/view_dispatcher.h>
#include <gui/scene_manager.h>
#include <notification/notification_messages.h>
#include <storage/storage.h>
#include <dialogs/dialogs.h>

#include <gui/modules/submenu.h>
#include <gui/modules/popup.h>
#include <gui/modules/loading.h>
#include <gui/modules/text_input.h>
#include <gui/modules/number_input.h>
#include <gui/modules/text_box.h>
#include <gui/modules/widget.h>

#include <input/input.h>
#include <lib/flipper_format/flipper_format.h>
#include <lib/toolbox/path.h>

#include <lib/nfc/nfc.h>
#include <nfc/nfc_poller.h>
#include <nfc/nfc_listener.h>
#include <nfc/nfc_device.h>
#include <nfc/protocols/mf_ultralight/mf_ultralight_poller.h>
#include <nfc/protocols/mf_ultralight/mf_ultralight_listener.h>

#include <lib/toolbox/stream/stream.h>
#include <lib/toolbox/stream/file_stream.h>

/* generated by fbt from .png files in images folder */
#include <weebo_icons.h>

#include <amiibo.h>

#include "weebo.h"
#include "weebo_common.h"
#include "scenes/weebo_scene.h"

#define WEEBO_TEXT_STORE_SIZE      128
#define WEEBO_FILE_NAME_MAX_LENGTH 64

#define NTAG215_SIZE     540
#define NFC3D_UID_OFFSET 0x1D4
#define PAGE_SIZE        4

enum WeeboCustomEvent {
    // Reserve first 100 events for button types and indexes, starting from 0
    WeeboCustomEventReserved = 100,

    WeeboCustomEventViewExit,
    WeeboCustomEventTextInputDone,
    WeeboCustomEventNumberInputDone,
    // Card writing
    WeeboCustomEventCardDetected,
    WeeboCustomEventWritingUserData,
    WeeboCustomEventWritingConfigData,
    WeeboCustomEventWriteSuccess,
    WeeboCustomEventWriteFailure,
    WeeboCustomEventWrongCard,
};

typedef void (*WeeboLoadingCallback)(void* context, bool state);

struct Weebo {
    ViewDispatcher* view_dispatcher;
    Gui* gui;
    NotificationApp* notifications;
    SceneManager* scene_manager;
    Storage* storage;

    char text_store[WEEBO_TEXT_STORE_SIZE + 1];
    FuriString* text_box_store;

    // Common Views
    Submenu* submenu;
    Popup* popup;
    Loading* loading;
    TextInput* text_input;
    NumberInput* number_input;
    TextBox* text_box;
    Widget* widget;
    DialogsApp* dialogs;

    Nfc* nfc;
    NfcPoller* poller;
    NfcListener* listener;
    NfcDevice* nfc_device;

    FuriString* load_path;
    char file_name[WEEBO_FILE_NAME_MAX_LENGTH + 1];

    bool keys_loaded;
    nfc3d_amiibo_keys keys;

    WeeboLoadingCallback loading_cb;
    void* loading_cb_ctx;

    uint8_t figure[NFC3D_AMIIBO_SIZE];
};

typedef enum {
    WeeboViewMenu,
    WeeboViewPopup,
    WeeboViewLoading,
    WeeboViewTextInput,
    WeeboViewNumberInput,
    WeeboViewTextBox,
    WeeboViewWidget,
} WeeboView;

void weebo_text_store_set(Weebo* weebo, const char* text, ...);

void weebo_text_store_clear(Weebo* weebo);

void weebo_blink_start(Weebo* weebo);

void weebo_blink_stop(Weebo* weebo);

void weebo_show_loading_popup(void* context, bool show);

void weebo_set_loading_callback(Weebo* weebo, WeeboLoadingCallback callback, void* context);
bool weebo_file_select(Weebo* weebo);
